<template>
  <div class="user-profile">
    <el-header class="header">
      <div class="header-content">
        <div class="logo">
          <h2>🏃‍♂️ 健身活动预约系统</h2>
        </div>
        <div class="nav">
          <el-button @click="$router.push('/user/home')" type="primary">首页</el-button>
          <el-button @click="$router.push('/user/activities')">活动列表</el-button>
          <el-button @click="$router.push('/user/bookings')">我的预约</el-button>
          <el-button @click="logout" type="danger">退出登录</el-button>
        </div>
      </div>
    </el-header>
    <el-main class="main-content">
      <el-card class="profile-card">
        <template #header>
          <div class="card-header">
            <span>👤 个人中心</span>
          </div>
        </template>

        <el-form :model="userForm" :rules="userRules" ref="userFormRef" label-width="100px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="userForm.username" disabled />
          </el-form-item>
          
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="userForm.phone" />
          </el-form-item>
          
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="userForm.email" />
          </el-form-item>
          
          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="userForm.gender">
              <el-radio :label="1">男</el-radio>
              <el-radio :label="2">女</el-radio>
            </el-radio-group>
          </el-form-item>
          
          <el-form-item>
            <el-button type="primary" @click="updateProfile" :loading="loading">
              {{ loading ? '更新中...' : '更新信息' }}
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-main>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { userApi } from '@/api'

const router = useRouter()

const loading = ref(false)
const userFormRef = ref()

const userForm = reactive({
  username: '',
  phone: '',
  email: '',
  gender: 1
})

const userRules = {
  phone: [
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  email: [
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ]
}

const loadUserInfo = async () => {
  try {
    // 这里应该从localStorage获取用户ID，暂时使用模拟数据
    const userId = 1
    const res = await userApi.getUserInfo(userId)
    const userInfo = res.data.user
    Object.assign(userForm, userInfo)
  } catch (error) {
    console.error('加载用户信息失败:', error)
    ElMessage.error('加载用户信息失败')
  }
}

const updateProfile = async () => {
  try {
    await userFormRef.value.validate()
    loading.value = true
    
    await userApi.updateUser(userForm)
    ElMessage.success('用户信息更新成功')
  } catch (error) {
    console.error('更新用户信息失败:', error)
  } finally {
    loading.value = false
  }
}

const logout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('userInfo')
  ElMessage.success('退出登录成功')
  router.push('/login')
}

onMounted(() => {
  loadUserInfo()
})
</script>

<style scoped>
.user-profile {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.header {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid #e0e0e0;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
}

.logo h2 {
  margin: 0;
  color: #333;
}

.nav .el-button {
  margin-left: 10px;
}

.main-content {
  padding: 20px;
}

.profile-card {
  max-width: 600px;
  margin: 0 auto;
  border-radius: 15px;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style> 